//
//  SCHomeController.m
//  Mutal
//
//  Created by TonyStart on 16/10/18.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCHomeController.h"
#import "SCHomeNavLeftView.h"
#import "SCHomeSearchView.h"
#import "SCNetworkTool.h"
#import "HeaderScrollerView.h"
#import "SCAlertView.h"
#import "SCHomeCell.h"
#import "SCHomeLuckyBagViewModel.h"
#import "SCWishViewModel.h"
#import "SCRefreshControl.h"
#import "SCWishInfoController.h"
@interface SCHomeController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>{
    NSIndexPath *luckyBagIndexPath;
}

@property(nonatomic,strong)SCHomeSearchView *searchView;
@property(nonatomic,strong)UIBarButtonItem *leftBarItem;
@property(nonatomic,strong)UIBarButtonItem *rightBarItem;
@property(nonatomic,strong)NSArray *scrollerImgArray;
@property(nonatomic,strong)HeaderScrollerView *headerScrollerView;
@property(nonatomic,strong)SCHomeLuckyBagViewModel *homeLuckyBagViewModel;
@property(nonatomic,strong)SCRefreshControl *homeRefreshC;
@property(nonatomic,strong)SCWishViewModel *wishViewModel;
@end

#define SCREEN_BOUNDS [UIScreen mainScreen].bounds
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size

@implementation SCHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadHomeData];
    [self loadHomeWishData];
    

    
}
-(SCRefreshControl *)homeRefreshC{
    if (!_homeRefreshC) {
        _homeRefreshC = [[SCRefreshControl alloc] init];
    
        _homeRefreshC.isAllowDrop = NO;
    }
    return _homeRefreshC;
}
//愿望视图模型懒加载
-(SCWishViewModel *)wishViewModel{
    if (!_wishViewModel) {
        _wishViewModel = [[SCWishViewModel alloc] init];
        
    }
    return _wishViewModel;
}
//加载数据
-(void)loadWishData{
    [self.homeRefreshC beginRefreshing:^(BOOL is_drop) {
        
    }];

    [self.wishViewModel requestWishsDataIS_TJ:1 finish:^() {
        
        [self.tableView reloadData];
        [self.homeRefreshC endRefreshing];
    }];
}
//头部滚动视图
-(HeaderScrollerView *)headerScrollerView{
    if (!_headerScrollerView) {
        _headerScrollerView = [[HeaderScrollerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 100)];
        _headerScrollerView.scrollImgArray = self.scrollerImgArray;
    }
    return _headerScrollerView;
}


//导航栏 顶部视图 搜索框
-(SCHomeSearchView *)searchView{
    if (!_searchView) {
        _searchView = [[SCHomeSearchView alloc] initWithFrame:CGRectMake(0, 0, 230, 35)];
        _searchView.searchField.placeholder = @"项目、用户";
        _searchView.searchField.delegate = self;
    }
    return _searchView;
}
//导航栏 顶部视图 右边栏
-(UIBarButtonItem *)rightBarItem{
    if (!_rightBarItem) {
        _rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(loginClick)];
    }
    return _rightBarItem;
}
//导航栏 顶部视图 左边栏
-(UIBarButtonItem *)leftBarItem{
    if (!_leftBarItem) {
        SCHomeNavLeftView *leftView = [[SCHomeNavLeftView alloc] initWithFrame:CGRectMake(0, 0, 70, 35)];
        leftView.backgroundColor = [UIColor whiteColor];
        _leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
        
    }
    return _leftBarItem;
}
//安装UI
-(void)setupUI{
    [self.tableView addSubview:self.homeRefreshC];
    [self loadWishData];
    self.navigationItem.titleView = self.searchView;
    self.navigationItem.leftBarButtonItem = self.leftBarItem;
    self.navigationItem.rightBarButtonItem = self.rightBarItem;
    
    self.tableView.estimatedRowHeight = 105;
 
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableHeaderView = self.headerScrollerView;
}
//输入框开始编辑的时候
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
    return NO;
}

//tableview 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        if (self.wishViewModel.wishArray.count>0) {
            return self.wishViewModel.wishArray.count;
        }else{
            return  0;
        }
        
    }
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identifier = nil;
    
    if (indexPath.section == 0) {
        identifier = @"firstCell";
    }else if (indexPath.section ==1){
        identifier = @"secondCell";
    }else if (indexPath.section == 2){
        identifier = @"threeCell";
    }
    
    SCHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
//    //????
//    if (indexPath.section == 0) {
//        cell.clickTT = ^{
//            _aaClick();
//        };
//    }
    
    if (indexPath.section == 1) {
        cell.luckyBagModel = self.homeLuckyBagViewModel.luckBagModel;
       
    }else if (indexPath.section == 2){
        
        if (self.wishViewModel.wishArray) {
            cell.wishModel = self.wishViewModel.wishArray[indexPath.row];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    
    
    
    return cell;
}
//跳转到愿望详情页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        SCWishInfoController *wishInfoVC = [UIStoryboard storyboardWithName:@"SCWishInfo" bundle:nil].instantiateInitialViewController;
        
        wishInfoVC.wish = self.wishViewModel.wishArray[indexPath.row];
        wishInfoVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:wishInfoVC animated:YES];
    }
}
//加载首页需要的数据
-(void)loadHomeData{
    _homeLuckyBagViewModel =  [[SCHomeLuckyBagViewModel alloc] init];
    [_homeLuckyBagViewModel requestHomeLuckBag:^(BOOL isLoad) {
        if (isLoad) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        }
    }];
}
//加载首页愿望列表
-(void)loadHomeWishData{
    
}
-(void)loginClick{
    
}

@end
